package com.hzhc.controller.trainplan;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.entity.TrainPlan;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.TbSequenceService;
import com.hzhc.service.TrainPlanService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.CalendarVo;
import com.hzhc.vo.TrainPlanVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/5/29 14:02
 * @description TrainPlanController
 */
@RestController
@RequestMapping("/trainPlan")
@RequiredArgsConstructor
public class TrainPlanController extends BaseController {

    private final TrainPlanService planService;

    private final TbSequenceService sequenceService;

    /**
     * 装卸车计划 list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getTrainPlanList")
    @ResponseBody
    public ResultUtil<List<TrainPlanVo>> getTrainPlanList(HttpServletRequest request, @RequestBody TrainPlanVo vo){
        Page<TrainPlanVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<TrainPlanVo> list = planService.getTrainPlanList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 装卸车计划 add
     * @param request
     * @param plan
     * @return
     */
    @PostMapping("/addTrainPlan")
    @SysTemLog(operModul = "调度管理-班列计划",operType = "ADD",operDesc = "班列计划添加",modular="PC")
    public ResultUtil<T> addTrainPlan(HttpServletRequest request,@RequestBody TrainPlan plan){
        Long userID = TokenUtil.getUserID(request);
        plan.setUserId(userID);
        plan.setCreateTime(LocalDateTime.now());
        String date = DateUtil.format(new Date(), "YYYYMMdd");
        String orderNo = sequenceService.hcseq_next_dayvalSuffix(3, "trainplanid");
        String a = "";
        if ("ZC".equals(plan.getType())){
            a = "F";
        }else{
            a = "X";
        }
        plan.setTrainPlanNo(date+orderNo+a);
        return planService.addTrainPlan(plan);
    }

    /**
     * 装卸车计划 update
     * @param request
     * @param plan
     * @return
     */
    @PostMapping("/updateTrainPlan")
    @SysTemLog(operModul = "调度管理-班列计划",operType = "UPDATE",operDesc = "班列计划修改",modular="PC")
    public ResultUtil<T> updateTrainPlan(HttpServletRequest request,@RequestBody TrainPlan plan){
        Long userID = TokenUtil.getUserID(request);
        plan.setUpdateUserId(userID);
        plan.setUpdateTime(LocalDateTime.now());
        return planService.updateTrainPlan(plan);
    }

    /**
     * 装卸车完工状态
     */
    @PostMapping("/wgTrainPlan")
    @SysTemLog(operModul = "调度管理-完工状态",operType = "UPDATE",operDesc = "完工状态修改",modular="PC")
    public ResultUtil<T> wgTrainPlan(HttpServletRequest request,@RequestBody TrainPlanVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUpdateUserId(userID);
        return planService.wgTrainPlan(vo);
    }

    /**
     * 装卸车计划 delete
     * @param id
     * @return
     */
    @GetMapping("/delTrainPlan/{id}")
    @SysTemLog(operModul = "调度管理-班列计划",operType = "DELETE",operDesc = "班列计划删除",modular="PC")
    public ResultUtil<T> deleteTrainPlan(@PathVariable Long id){
        return planService.deleteTrainPlan(id);
    }

    /**
     * 装卸车下发指令
     * @param id
     * @return
     */
    @GetMapping("/changeZl/{id}")
    public ResultUtil<T> updateChangeZl(@PathVariable Long id){
        return planService.updateChangeZl(id);
    }

    /**
     * 装卸车班列吞吐量统计
     * @param vo
     * @return
     */
    @RequestMapping("/getList")
    public ResultUtil<List<TrainPlanVo>> getList(@RequestBody TrainPlanVo vo) {
        List<TrainPlanVo> list= planService.selectInOutPortMonthList(vo);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 装卸车班列吞吐量统计 - 导出
     */
    @RequestMapping( "/exportMonthReport")
    @SysTemLog(operModul = "班列吞吐量统计-导出", operType = "导出", operDesc = "班列吞吐量统计导出功能", modular = "PC")
    public void exportMonthReport(TrainPlanVo vo, HttpServletRequest request, HttpServletResponse response) {
        planService.exportMonthReport(vo,request,response);
    }

    /**
     * 台账日历
     * @param data
     * @return
     */
    @RequestMapping("/getCalendarList")
    public ResultUtil<List<CalendarVo>> getCalendarList(@RequestBody JSONObject data){
        TrainPlanVo vo=data.to(TrainPlanVo.class);
//        vo.setType("ZC");  //卸+装
        return ResultUtil.build(planService.getCalendar(vo),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 卸车  堆存计划 区域修改
     * @param request
     * @param vo
     * @return
     */
    @PostMapping("/updateTrainPlanArea")
    @SysTemLog(operModul = "堆场管理-堆存计划",operType = "UPDATE",operDesc = "堆存计划修改",modular="PC")
    public ResultUtil<T> updateTrainPlanArea(HttpServletRequest request,@RequestBody TrainPlanVo vo){
        Long userId = TokenUtil.getUserID(request);
        vo.setUpdateUserId(userId);
        return planService.updateTrainPlanArea(vo);
    }

    /**
     * 发送通知
     * @param request
     * @param vo
     * @return
     */
    @PostMapping("/sendNotice")
    @SysTemLog(operModul = "调度管理-班列计划",operType = "UPDATE",operDesc = "班列计划发送短信通知",modular="PC")
    public ResultUtil<T> sendNotice(HttpServletRequest request,@RequestBody TrainPlanVo vo){
        Long userId = TokenUtil.getUserID(request);
        vo.setUserId(userId);
        return planService.updateSendNotice(vo);
    }


}
